<?php
    $item_menu[0] = 6; 
    $item_menu[1] = 1;  
    $title = 'Contratantes';
	
	include('header.php');

	if (!isset($_SESSION["admin"])) {redirect("login.php");	exit(); }

	$id = request('id',0);

	$accion = request('accion','');

	if($accion=='importar'){
		$usuario_id = request('usuario_id',0);
		$sql = "select * from usuarios where id='$usuario_id'";
		$rs = $db->SelectLimit($sql,1);
		$data = $rs->FetchRow();

	} elseif($accion=='guardar'){
		$data = $_POST['data'];
		$ok = $db->Replace('contratantes', $data,'id', $autoquote = true); 
		$letra = substr($data['apellido'],0,1);
		redirect('reservas-contratantes.php?letra='.$letra);
		die();

	} else {
		$sql = "select * from contratantes where id='$id'";
		$rs  = $db->SelectLimit($sql,1);
		$data = $rs->FetchRow();
	}
	
	
	
	
?>
	<h2>Nuevo contratante</h2>
	<?php if($id==0){?>
		<link rel="stylesheet" href="<?php echo URL;?>/css/ui.css" />
		<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
	
		<form class="form-search" method='post' action='<?php echo URL;?>/admin/reservas-contratantes_editar.php'>
			<table><tr>
				<td><legend>Importar de Usuarios:</legend></td>
				<td><input type="text" id="contratante" class="input-xlarge" name='contratante'></td>
				<td>
					<input type="hidden" id="usuario_id" name='usuario_id'>
					<input type="hidden" id="accion" name='accion' value='importar'>
					<button type="submit" name='buscar' class="btn">Importar</button>
				</td>
			</tr></table>
		</form>
		<script type="text/javascript">
			$(document).ready(function() {
				// Autocomplete
				$( "#contratante" ).autocomplete({
						source: "<?php echo URL;?>/ajax/buscar_usuarios.php",
						minLength: 2,
						select: function( event, ui ) {
							$( "#usuario_id" ).val( ui.item.id );
						}
				});
			});
		</script> 
	<?php } ?>


		<form method='post' action='<?php echo URL;?>/admin/reservas-contratantes_editar.php'>

		<div class="control-group">
			<label class="control-label" for="inputEmail">Email (*):</label>
			<div class="controls">
				<input type="hidden" name='data[id]' id="id" value='<?php echo $id;?>'>
				<input type="email" class='input-xlarge' name='data[email]' id="inputEmail"  value='<?php echo $data['email'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputNombre">Nombre (*):</label>
			<div class="controls">
			  <input type="text"  class='input-xlarge' name='data[nombre]' id="nombre"  value='<?php echo $data['nombre'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputApellido">Apellido (*):</label>
			<div class="controls">
			  <input type="text"  class='input-xlarge' name='data[apellido]' id="apellido"  value='<?php echo $data['apellido'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputApellido">Domicilio:</label>
			<div class="controls">
			  <input type="text"  class='input-xlarge' name='data[domicilio]' id="domicilio"  value='<?php echo $data['domicilio'];?>'>
			</div>
		</div>


		<div class="control-group">
			<label class="control-label" for="inputTelefono">Teléfono:</label>
			<div class="controls">
			  <input type="text"  class='input' name='data[telefono]' id="telefono" maxlength='60' value='<?php echo $data['telefono'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputTelefono">Localidad:</label>
			<div class="controls">
			  <input type="text"  class='input' name='data[localidad]' id="localidad" maxlength='60' value='<?php echo $data['localidad'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputTelefono">Provincia:</label>
			<div class="controls">
			  <input type="text"  class='input' name='data[provincia]' id="provincia" maxlength='60' value='<?php echo $data['provincia'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputTelefono">País:</label>
			<div class="controls">
			  <input type="text"  class='input' name='data[pais]' id="pais" maxlength='60' value='<?php echo $data['pais'];?>'>
			</div>
		</div>

		<div class="control-group">
			<label class="control-label" for="inputTelefono">Notas:</label>
			<div class="controls">
				<textarea rows='5' class='input-xxlarge' name='data[comentario]'><?php echo $data['comentario'];?></textarea>
			</div>
		</div>

		<input type='hidden' name='data[id]' value='<?php echo $id;?>' > 
		<input type='hidden' name='accion' value='guardar' > 
		<input type='submit' class='btn btn-primary' value='Guardar Cambios'>
	</form>	
		
		
<style>
.control-label{
	float:left;
	width:150px;
	text-align:right;
	font-weight:bold;
}
</style>		